<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
            <el-form-item label="所属行业" prop="industryId">
                <div class="w-[200px]">
                    <el-select class="flex-1" v-model="queryParams.industryId" placeholder="请选择所属行业" clearable>
                        <el-option class="flex-1" v-for="item in industryList" :key="item.industryId" :label="item.name"
                            :value="item.industryId" />
                    </el-select>
                </div>
            </el-form-item>
            <el-form-item label="所属系统" prop="systemName">
                <el-input style="width: 200px;" v-model="queryParams.systemName" placeholder="请输入所属系统" clearable
                    @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="商家名称" prop="merchantName">
                <el-input v-model="queryParams.merchantName" placeholder="请输入商家名称" clearable style="width: 200px"
                    @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-table v-loading="loading" :data="noticeList" @selection-change="handleSelectionChange">
            
            <!-- 
            序号、名称、门面照、所属行业、
            所属系统、单位地址、法人姓名、
            法人电话、单位电话、创建时间、
            状态、操作人员
            -->
            <el-table-column label="序号" align="center" type="index" width="55">
                <template #default="{ $index }">
                    {{ (queryParams.currentPage - 1) * queryParams.pageSize + $index + 1 }}
                </template>
            </el-table-column>
            <el-table-column label="商家名称" align="center" prop="name" />
            <el-table-column label="门面照片" width="100" align="center" prop="doorFace">
                <template #default="scope">
                    <el-image style="width: 50px; height: 50px;" :src="scope.row.doorFace"
                        :preview-src-list="[scope.row.doorFace]" preview-teleported>
                    </el-image>
                </template>
            </el-table-column>
            <el-table-column label="投放形式" align="center" class-name="small-padding fixed-width">
                <template #default="scope">
                    <el-button link type="primary" icon="edit" @click="handleUpdate(scope.row)">投放形式</el-button>
                </template>
            </el-table-column>
            <el-table-column label="所属行业" align="center" prop="industryName" />
            <el-table-column label="所属系统" align="center" prop="systemName" />
            <el-table-column label="单位地址" align="center" prop="addressDetail" />
            <el-table-column label="法人姓名" align="center" prop="legalPersonName" />
            <el-table-column label="法人电话" align="center" prop="legalPersonPhone" />
            <el-table-column label="单位电话" align="center" prop="officeSpacePhone" />
            <el-table-column label="创建时间" align="center" prop="createTime" />
            <el-table-column label="状态" align="center" prop="isPublish">
        <template #default="scope">
          <span v-if="scope.row.isPublish == true">已发布</span>
          <span v-if="scope.row.isPublish == false">未发布</span>
        </template>
      </el-table-column>
            <el-table-column label="操作人员" align="center" prop="updateUserName" />

        </el-table>
        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.currentPage"
            v-model:limit="queryParams.pageSize" @pagination="getList" />


                <!-- 添加或修改审核对话框 -->
    <el-dialog title="医院竞价排名投放详情" v-model="open" width="780px" append-to-body>
      <el-form ref="noticeRef" :model="form" :rules="rules" label-width="80px">
        <el-row>
            <!-- 
            医院竞价排名投放详情：
                投放范围
                开始日期
                列表位置
                用户端云平台界面医院列表
                结束日期
                位置排名
            -->
          <el-col :span="12">
            <el-form-item label="投放范围" prop="scope">
              <el-select v-model="form.scope" placeholder="">
                <el-option v-for="item in scopeList" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="开始日期" prop="startDate">
              <el-input v-model="form.startDate" placeholder="请输入开始日期" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="列表位置" prop="scope">
                <el-input value="用户端云平台界面医院列表" placeholder="请输入列表伪造" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="结束日期" prop="endDate">
              <el-input v-model="form.endDate" placeholder="请输入结束日期" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="位置排名" prop="sort">
              <el-input v-model="form.sort" placeholder="请输入排名" />
            </el-form-item>
          </el-col>


        </el-row>
      </el-form>
    </el-dialog>

    </div>
</template>

<script setup name="Notice">
import {
  authApiIndustryList
} from "@/api/login.js";

const industryList = ref([])
function authApiIndustryListApi() {
  authApiIndustryList().then(
    (res) => {
      industryList.value = [...res];
    }
  );
}

onMounted(() => {
  authApiIndustryListApi()
})

import { ElMessageBox } from 'element-plus'

import {
    advertisementMerchantApisuperviselist,
    advertisementMerchantApisupervisedetail,
} from "@/api/backAdvertisementMerchant/backAdvertisementMerchant";

let scopeList = [
  { label: "所在地一公里内发布", value: 1 },
  { label: "所在区县内发布", value: 2 },
  { label: "所在市内发布", value: 3 },
  { label: "所在省内发布", value: 4 },
  { label: "所在全国内发布", value: 5 },
]

const { proxy } = getCurrentInstance();

const noticeList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
    form: {},
    queryParams: {
        currentPage: 1,
        pageSize: 10,
        noticeTitle: undefined,
        createBy: undefined,
        status: undefined,
    },
    rules: {
        noticeTitle: [
            { required: true, message: "审核标题不能为空", trigger: "blur" },
        ],
        noticeType: [
            { required: true, message: "审核类型不能为空", trigger: "change" },
        ],
    },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询审核列表 */
function getList() {
    loading.value = true;
    advertisementMerchantApisuperviselist(queryParams.value).then((response) => {
        noticeList.value = response.records;
        total.value = response.total;
        loading.value = false;
    });
}

/** 取消按钮 */
function cancel() {
    open.value = false;
    reset();
}

/** 表单重置 */
function reset() {
    form.value = {
        advertisementMerchantId: undefined,
        noticeTitle: undefined,
        noticeType: undefined,
        noticeContent: undefined,
        status: "0",
    };
    proxy.resetForm("noticeRef");
}

/** 搜索按钮操作 */
function handleQuery() {
    queryParams.value.currentPage = 1;
    getList();
}

/** 重置按钮操作 */
function resetQuery() {
    proxy.resetForm("queryRef");
    handleQuery();
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
    ids.value = selection.map((item) => item.advertisementMerchantId);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
    reset();
    open.value = true;
    title.value = "添加审核";
}

/**修改按钮操作 */
function handleUpdate(row) {
    reset();
    const advertisementMerchantId = row.advertisementMerchantId || ids.value;
    advertisementMerchantApisupervisedetail(advertisementMerchantId).then((response) => {
        form.value = response;
        open.value = true;
        title.value = "投放形式";
    });
}

/** 提交按钮 */
function submitForm() {
    proxy.$refs["noticeRef"].validate((valid) => {
        if (valid) {
            if (form.value.advertisementMerchantId != undefined) {
                updateNotice(form.value).then((response) => {
                    proxy.$modal.msgSuccess("修改成功");
                    open.value = false;
                    getList();
                });
            } else {
                addNotice(form.value).then((response) => {
                    proxy.$modal.msgSuccess("新增成功");
                    open.value = false;
                    getList();
                });
            }
        }
    });
}

/** 删除按钮操作 */
function handleDelete(row) {
    const advertisementMerchantIds = row.advertisementMerchantId || ids.value;
    const advertisementContent = row.name || ids.value;
    ElMessageBox.confirm('是否确认审核内容为"' + advertisementContent + '"的数据项？', "系统提示", {
        confirmButtonText: '同意',
        cancelButtonText: '拒绝',
        type: "warning",
        distinguishCancelAndClose: true,
    }).then(function (e) {
        console.log(e, "then");
        advertisementMerchantApiApplyHandle({
            advertisementMerchantId: advertisementMerchantIds,
            handle: true
        });
        getList();
    }).catch((action) => {
        if (action == 'cancel') {
            advertisementMerchantApiApplyHandle({
            advertisementMerchantId: advertisementMerchantIds,
            handle: false
        });
        }
        getList();
    });
}

getList();
</script>